package com.vercon.commonmirror.presenter;


import org.reactivestreams.Subscription;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by SUE on 2016/7/20 0020.
 */
public class MirrorListPresenter<T> extends AbsPresenter {

    protected boolean isRefresh;
    private boolean isWithLoad;
    protected int pageNum;
    protected Subscription subscription;
    protected ListResultView<T> listResultView;
    private Observable observable;

    private Runnable questBeforeRunable;


    public void setListResultView(ListResultView<T> listResultView) {
        this.listResultView = listResultView;
    }

    public void refreshWithLoading() {
        isRefresh = true;
        isWithLoad = true;
        pageNum = 0;
        // listResultView.showLoading();
        query();
    }

    public void refresh() {
        isRefresh = true;
        isWithLoad = false;
        pageNum = 0;
        query();
    }

    public void loadMore() {
        isRefresh = false;
        isWithLoad = false;
        pageNum++;
        query();
    }

    /**
     * 针对音乐数据源，加载更多时，数据不够继续请求，不是加载更多的徐秀
     */
    public void keppLoad(){
        isRefresh = false;
        isWithLoad = false;
        query();
    }

    public void cancelRequest() {
        if (null != subscription) subscription.cancel();
    }

    public void query() {
        if (null != questBeforeRunable) {
            questBeforeRunable.run();
        }
        if (null == observable) return;
        if (null != subscription) subscription.cancel();
        getObservable().subscribe(new Observer<T>() {
            @Override
            public void onSubscribe(Disposable d) {
            }
            @Override
            public void onNext(T recommendVideos) {
                if (null != listResultView) {
                    if (isRefresh) {
                        listResultView.onSuccessRefresh(recommendVideos);
                    } else {
                        listResultView.onSuccessLoadMore(recommendVideos);
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                listResultView.onError(-1, e.getMessage());

                if (isWithLoad) {
                    //  listResultView.onLoadFailed();
                }
            }

            @Override
            public void onComplete() {

            }
        });
//        addSubscription(subscription);
    }


    public int getPageNum() {
        return pageNum;
    }

    public void setObservable(Observable observable) {
        this.observable = observable;
    }

    public Observable getObservable() {
        return observable;
    }

    public void setQuestBeforeRunable(Runnable questRunable) {
        this.questBeforeRunable = questRunable;
    }

    public interface ListResultView<T> {
        void onSuccessRefresh(T result);

        void onSuccessLoadMore(T result);

        void onError(int code, String message);
    }

    public class AbsListResultView<T> implements ListResultView<T> {


        @Override
        public void onSuccessRefresh(T result) {

        }

        @Override
        public void onSuccessLoadMore(T result) {

        }

        @Override
        public void onError(int code, String message) {

        }
    }

    public boolean isWithLoad() {
        return isWithLoad;
    }

    public boolean isRefresh() {
        return isRefresh;
    }
}
